package com.karaoke.android.kege.databinding

import android.databinding.BindingAdapter
import android.inputmethodservice.Keyboard
import android.inputmethodservice.KeyboardView
import android.net.Uri
import android.os.Environment
import android.widget.ImageView
import com.karaoke.android.data.onIO
import com.karaoke.android.data.onUI
import com.karaoke.android.kege.R
import rx.Observable
import rx.Observer
import java.io.File
import android.graphics.BitmapFactory
import android.graphics.Bitmap
import com.bumptech.glide.Glide
import com.facebook.common.executors.UiThreadImmediateExecutorService
import com.facebook.common.references.CloseableReference
import com.facebook.datasource.DataSource
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber
import com.facebook.imagepipeline.image.CloseableImage
import com.facebook.imagepipeline.request.ImageRequestBuilder


/**
 * Created by Allen on 16/9/2.
 */
object KeyboardViewBindingAdapter {
    @BindingAdapter("keyboard")
    @JvmStatic
    fun setKeyboard(keyboardView: KeyboardView,
                    resId: Int) {
        if (resId > 0)
            keyboardView.keyboard = Keyboard(keyboardView.context, resId)
    }


    @BindingAdapter("keyboardActionListener")
    @JvmStatic
    fun setKeyboardActionListener(keyboardView: KeyboardView, onKeyboardActionListener: KeyboardView.OnKeyboardActionListener?) {
        try {
            keyboardView.setOnKeyboardActionListener(onKeyboardActionListener)
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }

    @BindingAdapter("onOnKeyboardAction")
    @JvmStatic
    fun setOnKeyboardActionListener(keyboardView: KeyboardView,
                                    listener: KeyboardView.OnKeyboardActionListener?) {
        try {
            keyboardView.setOnKeyboardActionListener(listener)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    @BindingAdapter("uri")
    @JvmStatic
    fun setImageUri(view: ImageView,
                    uri: String?) {
        if (!uri.isNullOrBlank()) {
            val file = File(Environment.getExternalStorageDirectory(), uri)
            if (file.exists())
                Glide.with(view.context).load(file).placeholder(R.mipmap.singer_def).into(view)
            else
                loadImage(view, "http://main.oksrv.com" + uri)

        } else {
            view.setImageResource(R.mipmap.singer_def)
        }
    }

    fun loadImage(imageView: ImageView, uri: String?) {
        uri?.let {
            val imagePipeline = Fresco.getImagePipeline()
            val builder = ImageRequestBuilder.newBuilderWithSource(Uri.parse(it))
            val request = builder.build()
            val dataSource = imagePipeline.fetchDecodedImage(request, imageView.context)
            dataSource.subscribe(object : BaseBitmapDataSubscriber() {
                override fun onFailureImpl(dataSource: DataSource<CloseableReference<CloseableImage>>?) {
                }

                override fun onNewResultImpl(bitmap: Bitmap?) {
                    imageView.setImageBitmap(bitmap)
                }
            }, UiThreadImmediateExecutorService.getInstance())
        }

    }

    fun getBitmap(file: File): Bitmap? {
        if (file.exists()) {
            return BitmapFactory.decodeFile(file.absolutePath)

        }
        return null
    }
}